package com.baoai.util;

import com.baoai.MyIkAnalyzer;
import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.document.*;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.FSDirectory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;
import java.nio.file.Paths;

/**
 * Created by xiaren on 2017/4/6.
 */
public class LuenceUtils {
    private static final Logger LOGGER = LoggerFactory.getLogger(LuenceUtils.class);
    private static Directory directory;
    private static Analyzer analyzer;
    public static String filePath =
            "d:\\searchIndex";
//             "//local//wwwroot//search//index//searchIndex";
    static {
        try {
//            directory = FSDirectory.open(Paths.get(filePath));
             analyzer = new MyIkAnalyzer();
//             analyzer = new StandardAnalyzer();
//            analyzer = new SmartChineseAnalyzer();
        } catch (Exception e) {
            LOGGER.error("LuceneUtils error!", e);
        }
    }

    public static Directory getDirectory() {
        try {
            return FSDirectory.open(Paths.get(filePath));
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

    public static Analyzer getAnalyzer() {
        return analyzer;
    }

    public static void closeIndexWriter(IndexWriter indexWriter) {
        if (indexWriter != null) {
            try {
                indexWriter.close();
                if(indexWriter.getDirectory() != null){
                    try {
                        indexWriter.getDirectory().close();
                    } catch (IOException e) {
                        LOGGER.error("directory.close error", e);
                    }
                }
            } catch (Exception e) {
                LOGGER.error("indexWriter.close error", e);
            }
        }
    }





    /**
     * 用于添加权重
     *
     * @param document
     * @param name
     * @param value
     */
    public static void addIntPoint(Document document, String name, int value) {
        Field field = new IntPoint(name, value);
        document.add(field);
        //要排序，必须添加一个同名的NumericDocValuesField
        field = new NumericDocValuesField(name, value);
        document.add(field);
        //要存储值，必须添加一个同名的StoredField
        field = new StoredField(name, value);
        document.add(field);
    }

    /**
     * 用于添加权重
     *
     * @param document
     * @param name
     * @param value
     */
    public static void addDoublePoint(Document document, String name, Double value) {
        Field field = new DoublePoint(name, value);
        document.add(field);
        //要存储值，必须添加一个同名的StoredField
        field = new StoredField(name, value);
        document.add(field);
    }

    /**
     * 用于添加权重
     *
     * @param document
     * @param name
     * @param value
     */
    public static void addLongPoint(Document document, String name, Long value) {
        Field field = new LongPoint(name, value);
        document.add(field);
        //要排序，必须添加一个同名的NumericDocValuesField
        field = new NumericDocValuesField(name, value);
        document.add(field);
        //要存储值，必须添加一个同名的StoredField
        field = new StoredField(name, value);
        document.add(field);
    }


}
